<style>
.pure_table_extra { width: 750px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; cursor: pointer; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
.pure_table_extra tr:hover td { background: #ffff00; }
</style>

<?php 
$start = Utils::get_param('start', date('Y-m-d', strtotime('last monday', strtotime('tomorrow'))));
$end = date('Y-m-d', strtotime($start) + (86400 * 4));

$diploma = Utils::get_param('diploma', 0);
$language = Utils::get_param('language', 1);
$ncea = Utils::get_param('ncea', 0);
$ncea_english = Utils::get_param('ncea_english', 1);

$opts = new stdClass();
$opts->cd = '(t1.start <= :v1 AND t1.end >= :v1)';
$opts->cdv = array(':v1' => $start);
$opts->sel = 't.*, t1.start AS __start, t1.end AS __end, t2.id AS __logged';
$opts->jt = 'LEFT JOIN pb_colg_enrolled_class AS t1 ON t1.class_id = t.id LEFT JOIN pb_colg_atdnlog AS t2 ON t2.class_id = t.id AND t2.start = \'' . $start . '\'';
$opts->gb = 't.id';

if ($diploma == 1 || $language == 1 || $ncea == 1 || $ncea_english == 1) {
  $str = '';
  if ($diploma == 1) {
    $str .= (($str == '') ? '' : ' OR ') . 't.type = \'' . PT_DIPLOMA . '\'';
  }
  if ($language == 1) {
    $str .= (($str == '') ? '' : ' OR ') . 't.type = \'' . PT_LANGUAGE . '\'';
  }
  if ($ncea == 1) {
    $str .= (($str == '') ? '' : ' OR ') . 't.type = \'' . PT_NCEA . '\'';
  }
  if ($ncea_english == 1) {
    $str .= (($str == '') ? '' : ' OR ') . 't.type = \'' . PT_NCEA_ENGLISH . '\'';
  }
  $opts->cd .= " AND ($str)";
} else {
  $opts->cd .= " AND (false)";
}

$rslt = $orm->get('pb_colg_class', 't.name', 'ASC', NULL, NULL, $opts)->d;

$data = array();
foreach ($rslt as $itm) {
  $opts = new stdClass();
  $opts->cd = 't.class_id = :v1 AND (t2.start <= :v2 AND t2.end >= :v2)';
  $opts->cdv = array(':v1' => $itm->id, ':v2' => $start);
  $opts->sel = 'COUNT(t.id) AS num';
  $opts->jt = 'LEFT JOIN pb_colg_enrolled_intake AS t2 ON t2.id = t.enrolled_intake_id';
  if ($orm->get('pb_colg_enrolled_class', NULL, NULL, NULL, NULL, $opts)->d[0]->num > 0) {
    $data[] = $itm;
  }
}
?>

<div class="pure-form">
  <input id="start" type="text" value="<?php echo $start; ?>">&nbsp;
  <input id="end" type="text" value="<?php echo $end; ?>" readonly=true>
  <button style="display: none;" onclick="search();" class="pure-button">Search</button>
  <span id="types">
    <input onclick="search();" type="checkbox" id="diploma" /><label for="diploma"><?php echo PT_DIPLOMA; ?></label>
    <input onclick="search();" type="checkbox" id="language" /><label for="language" style="margin-left: -5px;"><?php echo PT_LANGUAGE; ?></label>
    <input onclick="search();" type="checkbox" id="ncea" /><label for="ncea" style="margin-left: -5px;"><?php echo PT_NCEA; ?></label>
    <input onclick="search();" type="checkbox" id="ncea_english" /><label for="ncea_english" style="margin-left: -5px;"><?php echo PT_NCEA_ENGLISH; ?></label>
  </span>
</div>
<div>&nbsp;</div>
<button class="pure-button pure-primary" onclick="download();">Download All</button>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th class="ui-widget-header">Class</th>
      <th style="width: 80px;" class="ui-widget-header">Type</th>
      <th style="width: 150px;" class="ui-widget-header">Intake</th>
      <th style="width: 120px;" class="ui-widget-header">Teacher</th>
      <th style="width: 40px;" class="ui-widget-header">Saved</th>
      <th style="width: 40px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="class_body"></tbody>
</table>
<div>&nbsp;</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';
  var __atdnurl = '<?php echo ColgUtils::get_atdnurl($app_data); ?>';
  var __atdnurl2 = '<?php echo ColgUtils::get_atdnurl2($app_data); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __start = '<?php echo $start; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');

  var __diploma = '<?php echo $diploma; ?>';
  var __language = '<?php echo $language; ?>';
  var __ncea = '<?php echo $ncea; ?>';
  var __ncea_english = '<?php echo $ncea_english; ?>';
  
  $(function() {
    $('#types').buttonset();
    
    render();
  });
  
  function render() {
    $('#start').datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      beforeShowDay: function(date) {
        var day = date.getDay();
        return [(day == 1), ''];
      },
      onSelect: function(dateText) {
        search();
      }
    });
    render_data();
  };
  
  function render_data() {
    if (__data) {
      Url.insert_param('start', __start);

      Url.insert_param('diploma', __diploma);
      Url.insert_param('language', __language);
      Url.insert_param('ncea', __ncea);
      Url.insert_param('ncea_english', __ncea_english);
    }

    if (__diploma == 1) {
      $('#diploma').prop('checked', 'checked');
    } else {
      $('#diploma').removeAttr('checked');
    }
    if (__language == 1) {
      $('#language').prop('checked', 'checked');
    } else {
      $('#language').removeAttr('checked');
    }
    if (__ncea == 1) {
      $('#ncea').prop('checked', 'checked');
    } else {
      $('#ncea').removeAttr('checked');
    }
    if (__ncea_english == 1) {
      $('#ncea_english').prop('checked', 'checked');
    } else {
      $('#ncea_english').removeAttr('checked');
    }
    $('#types').buttonset('refresh');
    
    $('#class_body').empty();
    if (__data == null) {
      $('#class_body').append('<tr class="pure-table-even"><td colspan=13>Search for data</td></tr>');
      return;
    }
    for (var idx in __data) {
      var itm = __data[idx];
      $('#class_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.course_name + ' ' + itm.name + '</td>' +
          '<td>' + itm.type + '</td>' +
          '<td>' + ((itm.intake_id != 0) ? itm.__start + ' to ' + itm.__end : '') + '</td>' +
          '<td>' + ((itm.teacher_id != 0) ? itm.teacher_name : '') + '</td>' +
          '<td>' + ((itm.__logged) ? '<span style="color: green">Yes</span>' : '<span style="color: red">No</span>') + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-arrowthick-1-s" onclick="window.open(__atdnurl2 + \'start=\' + Utils.encodeURI($(\'#start\').val()) + \'&id=' + itm.id + '\');"></span>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_class&id=' + itm.id + '&start=' + __start + '\');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.length == 0) {
      $('#class_body').append('<tr class="pure-table-even"><td colspan=13>None</td></tr>');
    }
  };

  function search() {
    Url.redirect(__redmd, __sys + '.php?i=' + __id + '&start=' + Utils.encodeURI($('#start').val()) + '&diploma=' + ($('#diploma').is(':checked') ? 1 : 0) + '&language=' + ($('#language').is(':checked') ? 1 : 0) + '&ncea=' + ($('#ncea').is(':checked') ? 1 : 0) + '&ncea_english=' + ($('#ncea_english').is(':checked') ? 1 : 0));
  };

  function download() {
    window.open(__atdnurl + 'start=' + Utils.encodeURI($('#start').val()));
  };
  /*]]>*/
</script>